package com.framework.shs.a.service;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.framework.shs.a.dao.UserDao;
import com.framework.shs.a.entity.User;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	@Override
	public User test() {
		return userDao.test();
	}

	/**
	 * 判断用户是否登录
	 * 
	 * @param currUser
	 * @return
	 */
	@Override
	public String isLogin(User currUser) {
		Subject user = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(
				currUser.getName(), currUser.getPassword());
		token.setRememberMe(true);
		try {
			user.login(token);
			return "redirect:/main";
		} catch (AuthenticationException e) {
			token.clear();
			return "redirect:/login";
		}
	}
	@Override
	public User findByName(String username) {
		return userDao.findByName(username);
	}
	@Override
	public void updateUser(User currUser) {
		userDao.update(currUser);
	}
}
